package enron;

import java.awt.Component;

import javax.swing.DefaultCellEditor;
import javax.swing.JCheckBox;
import javax.swing.JTable;

public class ApplianceCellEditor extends DefaultCellEditor {
		public ApplianceCellEditor(JCheckBox checkBox) {
		super(checkBox);
	}
		private static final long serialVersionUID = 5816566428031713127L;
		
		
		/* (non-Javadoc)
		 * @see javax.swing.DefaultCellEditor#getTableCellEditorComponent(javax.swing.JTable, java.lang.Object, boolean, int, int)
		 */
		@Override
		public Component getTableCellEditorComponent(
				JTable table, Object value,
				boolean isSelected, int row, int column) {
			JCheckBox editor;
			if(value != null && Appliance.class.isInstance(value)) {
				//System.out.println("isAppliance" + ((Appliance)value))
				editor = (JCheckBox) super.getTableCellEditorComponent(table, null, isSelected, row, column);
				editor.setText(((Appliance)value).getDisplay());
				editor.setSelected(((Appliance)value).isTurnedOn());
				editor.putClientProperty(Appliance.class, value);
			} else {
				editor = (JCheckBox) super.getTableCellEditorComponent(table, null, isSelected, row, column);
			}
			return editor;
		}
		/* (non-Javadoc)
		 * @see javax.swing.DefaultCellEditor#getCellEditorValue()
		 */
		@Override
		public Object getCellEditorValue() {
			Object returnVal = ((JCheckBox)this.getComponent()).getClientProperty(Appliance.class);
			if(returnVal == null) {
				returnVal = super.getCellEditorValue();
			} else {
				((Appliance)returnVal).setTurnedOn((Boolean) super.getCellEditorValue());
			}
			return returnVal;
		}
}
